//全部好友资料
const getAllFriend = (identifier) => {
    return new Promise((resolve,reject) =>{
    let options = {
      From_Account: identifier,
      TimeStamp: 0,
      StartIndex: 0,
      GetCount: 100, //totalCount,
      LastStandardSequence: 0,
      TagList: [
        "Tag_Profile_IM_Nick",
        "Tag_Profile_IM_SelfSignature", //个性签名
        "Tag_Profile_IM_Image", //头像
        "Tag_Profile_IM_Gender", //性别
        "Tag_Profile_IM_AllowType", //加好友验证方式
        "Tag_Profile_IM_Location", //所在地
        "Tag_Profile_IM_BirthDay", //生日
        "Tag_Profile_IM_MsgSettings" //消息设置 置0表示接收消息，置1则不接收消息
      ]
    };
    webim.getAllFriend(
      options,
      resp => {
        if (resp.FriendNum > 0) {
            resolve(resp);
        }else{
            resolve(null);
        }
      },
      err => {
        reject(err.ErrorInfo);
      }
    );
   });
}
  
//读取用户个人资料
const getProfilePortrait = idArr => {
    return new Promise((resolve,reject) => {
    let tag_list = [
        "Tag_Profile_IM_Nick",//昵称
        "Tag_Profile_IM_Gender",//性别
        "Tag_Profile_IM_AllowType",//加好友方式
        "Tag_Profile_IM_Image"//头像
    ];
    let options = {
        'To_Account': idArr,
        'TagList': tag_list
    };
    webim.getProfilePortrait(
        options,
        resp => {
            let data = [];
            if (resp.UserProfileItem && resp.UserProfileItem.length > 0) {
                for (let i in resp.UserProfileItem) {
                    let to_account = resp.UserProfileItem[i].To_Account;
                    let nick = null, gender = null, allowType = null,imageUrl=null;
                    for (let j in resp.UserProfileItem[i].ProfileItem) {
                        switch (resp.UserProfileItem[i].ProfileItem[j].Tag) {
                            case 'Tag_Profile_IM_Nick':
                                nick = resp.UserProfileItem[i].ProfileItem[j].Value;
                                break;
                            case 'Tag_Profile_IM_Gender':
                                switch (resp.UserProfileItem[i].ProfileItem[j].Value) {
                                    case 'Gender_Type_Male':
                                        gender = '男';
                                        break;
                                    case 'Gender_Type_Female':
                                        gender = '女';
                                        break;
                                    case 'Gender_Type_Unknown':
                                        gender = '未知';
                                        break;
                                }
                                break;
                            case 'Tag_Profile_IM_AllowType':
                                switch (resp.UserProfileItem[i].ProfileItem[j].Value) {
                                    case 'AllowType_Type_AllowAny':
                                        allowType = 1; //'允许任何人';
                                        break;
                                    case 'AllowType_Type_NeedConfirm':
                                        allowType = 2; //'需要确认';
                                        break;
                                    case 'AllowType_Type_DenyAny':
                                        allowType = 3; //'拒绝任何人';
                                        break;
                                    default:
                                        allowType = '需要确认';
                                        break;
                                }
                                break;
                            case 'Tag_Profile_IM_Image':
                                imageUrl = resp.UserProfileItem[i].ProfileItem[j].Value;
                                break;
                        }
                    }
                    data.push({
                        'To_Account': to_account,
                        'Nick': webim.Tool.formatText2Html(nick),
                        'Gender': gender,
                        'AllowType': allowType,
                        'Image': imageUrl
                    });
                    resolve(data);
                }
            }
        },
        err => {
            reject(err.ErrorInfo);
            //alert(err.ErrorInfo);
        }
    );
  });
};

// 我的黑名单
const getBlackList = identifier => {
    return new Promise((resolve,reject) => {
    let options = {
      'From_Account': identifier,
      'StartIndex': 0,
      'MaxLimited': 100,
      'LastSequence': 0
    };

    webim.getBlackList(
      options,
      resp => {
        let arr = [];
        if (resp.BlackListItem && resp.BlackListItem.length > 0) {
          for (let i = 0; i < resp.BlackListItem.length; i++) {
            arr.push(resp.BlackListItem[i].To_Account)
          }
        }
        resolve(arr);
      },
      err => {
        reject(err.ErrorInfo);
      }
    );
   });
  };
//读取群组成员
const getGroupMemberInfo = group_id=> {
    return new Promise((resolve,reject) => {
    let options = {
        'GroupId': group_id,
        'Offset': 0, //必须从0开始
        'Limit': 2000,
        'MemberInfoFilter': [
            'Account',
            'Role',
            'JoinTime',
            'LastSendMsgTime',
            'ShutUpUntil'
        ]
    };
    webim.getGroupMemberInfo(
            options,
            resp => {
                if (resp.MemberNum <= 0) {
                    //alert('该群组目前没有成员');
                    return;
                }
                resolve(resp);
            },
            err => {
                reject(err);
            }
    );
   });
};

//获取我的群组
const getMyGroup = identifier => {
    return new Promise((resolve,reject) => {
    let options = {
        'Member_Account': identifier,
        'Limit': 200,
        'Offset': 0,
        //'GroupType':'',
        'GroupBaseInfoFilter': [
            'Type',
            'Name',
            'Introduction',
            'Notification',
            'FaceUrl',
            'CreateTime',
            'Owner_Account',
            'LastInfoTime',
            'LastMsgTime',
            'NextMsgSeq',
            'MemberNum',
            'MaxMemberNum',
            'ApplyJoinOption',
            'ShutUpAllMember'
        ],
        'SelfInfoFilter': [
            'Role',
            'JoinTime',
            'MsgFlag',
            'UnreadMsgNum'
        ]
    };
    webim.getJoinedGroupListHigh(
            options,
            resp => {
                if (!resp.GroupIdList || resp.GroupIdList.length == 0) {
                    //alert('你目前还没有加入任何群组');
                    return;
                }
                resolve(resp.GroupIdList);
            },
            err => {
                reject(err);
            }
    );
  });
};

//读取群组基本资料-高级接口
const getGroupInfo = function(group_id, cbOK, cbErr) {
    let options = {
        'GroupIdList': [
            group_id
        ],
        'GroupBaseInfoFilter': [
            'Type',
            'Name',
            'Introduction',
            'Notification',
            'FaceUrl',
            'CreateTime',
            'Owner_Account',
            'LastInfoTime',
            'LastMsgTime',
            'NextMsgSeq',
            'MemberNum',
            'MaxMemberNum',
            'ApplyJoinOption',
            'ShutUpAllMember'
        ],
        'MemberInfoFilter': [
            'Account',
            'Role',
            'JoinTime',
            'LastSendMsgTime',
            'ShutUpUntil'
        ]
    };
    webim.getGroupInfo(
        options,
        resp => {
            if (resp.GroupInfo[0].ShutUpAllMember == 'On') {
                //alert('该群组已开启全局禁言');
            }
            if (cbOK) {
                cbOK(resp);
            }
        },
        err => {
            //alert(err.ErrorInfo);
        }
    );
};
const Chat = {
    getAllFriend,
    getGroupInfo,
    getProfilePortrait,
    getBlackList,
    getGroupMemberInfo,
    getMyGroup
}
export default Chat